/**
 * 
 */
package org.cuatrovientos.erp.controller;


import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.BOGenericInterface;
import org.cuatrovientos.erp.business.StateBO;
import org.cuatrovientos.erp.model.State;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * Controller class form State management
 * @author Enrico Martinez
 *
 */
@Controller
public class StateController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(StateController.class);

	@Autowired
    private StateBO stateBO;
    
        /**
         * default constructor
         */
        public StateController () {
               logger.info("State BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /state or /state/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/state","/state/list"})
         */
        @RequestMapping({"/state","/state/list"})
        public ModelAndView stateDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<State> stateList = stateBO.selectAll();
                model.put("states", stateList );
                return new ModelAndView("StateList", model);     
        }



        /**
         * handles delete state, and redirect to state default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/state","/state/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/state/delete"})
        public String deleteState(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

            stateBO.deleteById(id);
            return "redirect:/state";

        }
        
        /**
         * handles new state request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "state"
         * @return the name of the view to show
         * @RequestMapping({"/state/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/state/new"})
        public ModelAndView newState() {
        		logger.info("new state ");
                State state = new State();
               
                return new ModelAndView("NewState", "state", state);
        }
        
        /**
         * handles new state request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/state/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/state/save"})
        public String newState(@Valid State state, BindingResult bindingResult) {
        		logger.info("new state " + state.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("state", state);
        			return "NewState";
        		}
        		
        		// If data is ok, insert and go on
                stateBO.insert(state);
                return "redirect:/state";
        }
        
        /**
         * handles delete state, and redirect to state default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/state","/state/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/state/detail"})
        public ModelAndView stateDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            Object state = stateBO.selectById(id);
            model.put("state", state);
            
            return new ModelAndView("StateDetail",model);

        }
        
        /**
         * handles state update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/state/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/state/update"})
        public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                Object state = stateBO.selectById(id);
                model.put("state", state);
               return new ModelAndView("UpdateState",model);

        }
        
        /**
         * handles save update state request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/state/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/state/saveupdate"})
        public String saveUpdate(@Valid State state, BindingResult bindingResult) {
        		logger.info("update state " + state.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("state", state);
        			return "UpdateState";
        		}
        		
                stateBO.update(state);
                return "redirect:/state";
        }
}
